package com.maori.languageapp.fragments;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONObject;
import com.maori.languageapp.R;
import com.maori.languageapp.Maori_Categories;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager.OnBackStackChangedListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Maori_Fragment_Curate extends Fragment {
	TextView noOfCountWord, noOfCountPhrase, noOfCountStory, noOfCountSong,
			noOfCountIdiom;
	int timeoutConnection = 5000;
	InputStream is = null;
	StringBuilder sb = null;
	JSONArray jArray;
	HttpParams httpParameters = new BasicHttpParams();
	String result;
	JSONArray jsonarray;
	ArrayList<HashMap<String, String>> arraylist;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.maori_fragment_curate, null);

		RelativeLayout captureWord = (RelativeLayout) view
				.findViewById(R.id.discover_word);
		RelativeLayout capturePhrase = (RelativeLayout) view
				.findViewById(R.id.discover_phrase);
		RelativeLayout captureStory = (RelativeLayout) view
				.findViewById(R.id.discover_story);
		RelativeLayout captureSong = (RelativeLayout) view
				.findViewById(R.id.discover_songs);
		RelativeLayout captureIdiom = (RelativeLayout) view
				.findViewById(R.id.discover_idioms);

		noOfCountWord = (TextView) view.findViewById(R.id.noOfCountWord);
		noOfCountPhrase = (TextView) view.findViewById(R.id.noOfCountPhrase);
		noOfCountStory = (TextView) view.findViewById(R.id.noOfCountStory);
		noOfCountSong = (TextView) view.findViewById(R.id.noOfCountSong);
		noOfCountIdiom = (TextView) view.findViewById(R.id.noOfCountIdiom);

		new callCurateCounter().execute();
		// Display Option
		// Display_Types 1 Discover ***** Index : 300
		// Display_Types 2 Capture ***** Index : 200
		// Display_Types 3 Curate ***** Index : 300

		captureWord.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg) {
				// Intent i = new Intent(getActivity(),
				// Maori_Capture_Word.class);
				Intent i = new Intent(getActivity(), Maori_Categories.class);
				i.putExtra("Display_Type", "300");
				i.putExtra("Capture_Type", "1");
				i.putExtra("Category", "CURATE");
				startActivity(i);
				getActivity().finish();
			}
		});

		capturePhrase.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg) {
				// Intent i = new Intent(getActivity(),
				// Maori_Capture_Phrase.class);
				Intent i = new Intent(getActivity(), Maori_Categories.class);
				i.putExtra("Display_Type", "300");
				i.putExtra("Capture_Type", "2");
				i.putExtra("Category", "CURATE");
				startActivity(i);
				getActivity().finish();
			}
		});

		captureStory.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg) {
				// Intent i = new Intent(getActivity(),
				// Maori_Capture_Story.class);
				Intent i = new Intent(getActivity(), Maori_Categories.class);
				i.putExtra("Display_Type", "300");
				i.putExtra("Capture_Type", "3");
				i.putExtra("Category", "CURATE");
				startActivity(i);
				getActivity().finish();
			}
		});

		captureSong.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg) {
				// Intent i = new Intent(getActivity(),
				// Maori_Capture_Song.class);
				Intent i = new Intent(getActivity(), Maori_Categories.class);
				i.putExtra("Display_Type", "300");
				i.putExtra("Capture_Type", "4");
				i.putExtra("Category", "CURATE");
				startActivity(i);
				getActivity().finish();
			}
		});

		captureIdiom.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg) {
				// Intent i = new Intent(getActivity(),
				// Maori_Capture_Idiom.class);
				Intent i = new Intent(getActivity(), Maori_Categories.class);
				i.putExtra("Display_Type", "300");
				i.putExtra("Capture_Type", "5");
				i.putExtra("Category", "CURATE");
				startActivity(i);
				getActivity().finish();
			}
		});
		
		return view;
	}

	private class callCurateCounter extends AsyncTask<Void, Void, Void> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			try {
				HttpConnectionParams.setConnectionTimeout(httpParameters,
						timeoutConnection);
				HttpClient httpclient = new DefaultHttpClient(httpParameters);
				HttpPost httppost = new HttpPost(
						"http://langapp.mirzabros.com/maoriapi/curate/getFilteredCuratePendingApprove.php");
				JSONObject json = new JSONObject();
				JSONArray postjson = new JSONArray();
				postjson.put(json);
				httppost.setHeader("json", json.toString());
				httppost.getParams().setParameter("jsonpost", postjson);
				httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
				HttpResponse response = httpclient.execute(httppost);
				HttpEntity entity = response.getEntity();
				is = entity.getContent();

				if (is != null) {

					BufferedReader reader = new BufferedReader(
							new InputStreamReader(is, "iso-8859-1"), 8);
					sb = new StringBuilder();
					sb.append(reader.readLine() + "\n");
					String line = "0";

					while ((line = reader.readLine()) != null) {
						sb.append(line + "\n");
					}
					is.close();
					result = sb.toString();
				}
			} catch (Exception e) {

			}
			return null;
		}

		@Override
		protected void onPostExecute(Void args) {
			if (result != null) {
				arraylist = new ArrayList<HashMap<String, String>>();
				try {
					JSONObject jsonObj = new JSONObject(result);
					jsonarray = jsonObj.getJSONArray("posts");
					for (int i = 0; i < jsonarray.length(); i++) {
						JSONObject c = jsonarray.getJSONObject(i);
						String WordPendingApprove = c
								.getString("WordPendingApprove");
						String PhrasePendingApprove = c
								.getString("PhrasePendingApprove");
						String StoryPendingApprove = c
								.getString("StoryPendingApprove");
						String SongPendingApprove = c
								.getString("SongPendingApprove");
						String IdiomPendingApprove = c
								.getString("IdiomPendingApprove");

						noOfCountWord.setText(WordPendingApprove);
						noOfCountPhrase.setText(PhrasePendingApprove);
						noOfCountStory.setText(StoryPendingApprove);
						noOfCountSong.setText(SongPendingApprove);
						noOfCountIdiom.setText(IdiomPendingApprove);
					}
				} catch (Exception e) {

				}
			}
		}

	}

	// ***********Memory Handling****************
	@Override
	public void onStop() {
		super.onStop();
	}

	// Fires after the OnStop() state
	@Override
	public void onDestroy() {
		super.onDestroy();
		try {
			trimCache(this);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void trimCache(Maori_Fragment_Curate maori_Fragment_Discover) {
		try {
			File dir = maori_Fragment_Discover.getCacheDir();
			if (dir != null && dir.isDirectory()) {
				deleteDir(dir);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	private File getCacheDir() {
		// TODO Auto-generated method stub
		return null;
	}

	public static boolean deleteDir(File dir) {
		if (dir != null && dir.isDirectory()) {
			String[] children = dir.list();
			for (int i = 0; i < children.length; i++) {
				boolean success = deleteDir(new File(dir, children[i]));
				if (!success) {
					return false;
				}
			}
		}

		// The directory is now empty so delete it
		return dir.delete();
	}

	void OnBackStackChangedListener() {

	}
}
